package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.RoleInfoEntity;
import com.cheng.data.mapper.RoleInfoMaper;
import com.cheng.data.vo.PermInfoVO;
import com.cheng.data.vo.RoleInfoVO;
import com.cheng.service.RoleInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/11/14 12:16
 * @version: 1.0
 * @description: 角色信息表操作
 */
@Service
public class RoleInfoServiceImpl implements RoleInfoService {

    private final static Logger log = LoggerFactory.getLogger(RoleInfoServiceImpl.class);

    @Autowired
    private RoleInfoMaper roleInfoMaper;

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: java.util.List<com.cheng.data.entity.RoleInfoEntity>
    * @date: 2021/11/14
    * @description: 查询角色信息列表
    */
    @Override
    public List<RoleInfoEntity> queryRoleInfoList(RoleInfoEntity entity) throws CommonException {

        QueryWrapper<RoleInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(entity.getRoId()),"ro_Id",entity.getRoId());
        wrapper.eq(StringUtil.isNotNull(entity.getRoCode()),"ro_Code",entity.getRoCode());
        wrapper.eq(StringUtil.isNotNull(entity.getRoNm()),"ro_Nm",entity.getRoNm());
        wrapper.like(StringUtil.isNotNull(entity.getRoDesc()),"ro_Desc",entity.getRoDesc());
        wrapper.orderByAsc("ro_Id");
        return roleInfoMaper.selectList(wrapper);
    }

    /**
     * @author: ChengJW
     * @params: [roCode]
     * @return: java.lang.String
     * @date: 2021/11/14
     * @description: 根据角色所属机关代码查询角色名称
     */
    @Override
    public String queryRoleInfoNmByCode(String roCode) throws CommonException {

        RoleInfoEntity entity = new RoleInfoEntity();
        entity.setRoCode(roCode);
        List<RoleInfoEntity> list = queryRoleInfoList(entity);
        if (null != list && list.size() > 0){
            return list.get(0).getRoDesc();
        }
        return null;
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: java.util.List<com.cheng.data.vo.PermInfoVO>
     * @date: 2021/11/16
     * @description: 根据角色信息查询菜单权限信息
     */
    @Override
    public List<PermInfoVO> queryAllPermInfoByRoleInfo(RoleInfoVO roleInfoVO) throws CommonException {

        Map<String,Object> map = new HashMap<>();
        map.put("roId",roleInfoVO.getRoId());
        return roleInfoMaper.queryRoleInfoList(map);
    }

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/11/17
     * @description: 新增角色信息
     */
    @Override
    public int insertRoleInfo(RoleInfoEntity entity) throws CommonException {
        return roleInfoMaper.insert(entity);
    }

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/11/17
     * @description: 修改角色信息
     */
    @Override
    public int updateRoleInfo(RoleInfoEntity entity) throws CommonException {
        return roleInfoMaper.updateById(entity);
    }

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/11/17
     * @description: 删除角色信息
     */
    @Override
    public int deleteRoleInfo(RoleInfoEntity entity) throws CommonException {
        return roleInfoMaper.deleteById(entity.getRoId());
    }

    /**
    * @author: ChengJW
    * @params: [roId]
    * @return: com.cheng.data.entity.RoleInfoEntity
    * @date: 2021/11/17
    * @description: 根据角色 ID 查询角色信息
    */
    @Override
    public RoleInfoEntity queryRoleInfoById(String roId) throws CommonException {
        return roleInfoMaper.selectById(roId);
    }
}
